﻿@using cloudscribe.Core.Web
@inject ISiteContextResolver siteResolver
@inject ICoreThemeHelper themeHelper
@inject IStringLocalizer<CloudscribeCore> sr

@{
    var site = await siteResolver.ResolveSite(Context.Request.Host.Host, Context.Request.Path);

    // Check if auto-logout is enabled and calculate initial time
    // IMPORTANT: Only enable if user is already authenticated
    var isAutoLogoutEnabled = false;
    var secondsTilLogout = 0.0;

    if (Context.User?.Identity?.IsAuthenticated == true &&
        site != null && 
        !string.IsNullOrWhiteSpace(site.MaximumInactivityInMinutes) && 
        double.TryParse(site.MaximumInactivityInMinutes, out var minutes) && 
        minutes > 0)
    {
        isAutoLogoutEnabled = true;
        // Assume full session time remaining on initial page load
        secondsTilLogout = minutes * 60; 
    }
}

@if (isAutoLogoutEnabled && secondsTilLogout > 0)
{
    Html.Resource(@<script src="/cr/js/cloudscribe-autologout-warning.min.js"></script>, "js");

    var pageScheme = Context.Request.Scheme; 
    

    <div 
        id="sessionExpiry"
        data-url-target="@Url.Action("AutoLogoutNotification", "Account", null, pageScheme)"
        data-alert-threshold="60"
        data-seconds-left="@secondsTilLogout">
    </div>

    <div class="modal" id="sessionExpiryWarning" tabindex="-1" role="dialog" aria-labelledby="sessionExpiryWarning" data-url-keep-alive="@Url.Action("RemainingSessionTime", "Account")">
        <div class="modal-dialog modal-sm" role="document">
            <div class="modal-content alert alert-warning">
                <div class="modal-header">
                    <h4 class="modal-title">@sr["Session Expiry"]</h4>
                    <button type="button" data-bs-dismiss="modal" class="btn-close" aria-label='@sr["Close"]'></button>
                </div>
                <div class="modal-body">
                    <span class="pull-left me-2"><i class="glyphicon glyphicon-warning-sign fas fa-exclamation-triangle fa-2x" aria-hidden="true"></i></span>
                    @Html.Raw(String.Format(sr["For your security, your session will time out in {0} seconds..."], "<b><span id=\"sessionExpiryWarningSeconds\"></span></b>"))
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary ms-2 mb-1 me-2" id="btnSessionLogOut">@sr["Log out"]</button>
                    <button type="button" class="btn btn-primary ms-2 mb-1" id="sessionKeepAlive">@sr["Stay logged in"]</button>
                </div>
            </div>
        </div>
    </div>
}
@* If auto-logout is disabled, render nothing *@